package com.onenine.app.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.onenine.app.R;
import com.onenine.app.event.CommentSubEvent;
import com.onenine.app.model.CommentParent;
import com.onenine.app.model.CommentSub;
import com.onenine.app.utils.CDataProcessor;
import com.onenine.app.utils.CHelper;
import com.onenine.app.utils.DataType;
import com.onenine.app.utils.GlideUtils;
import com.onenine.app.utils.RoleResLoader;
import com.onenine.app.view.CommentListView;
import com.onenine.app.view.NVoiceBar;
import com.onenine.app.view.likebutton.LikeButton;
import com.onenine.app.view.likebutton.OnLikeListener;
import com.onenine.chat.util.SimpleCommonUtils;
import com.onenine.common.util.ApUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * 评论数据适配器
 * 文本和语音2种类型
 */
public class PostCommentAdapter extends BaseMultiItemQuickAdapter<CommentParent, BaseViewHolder> {

    private String mPostId;
    private int mPostType;
    private int avatarSize;

    public PostCommentAdapter(List<CommentParent> dataList) {
        super(dataList);
        avatarSize = ApUtils.getApp().getResources().getDimensionPixelSize(R.dimen.avatar_size_38dp);
        addItemType(DataType.COMMENT_DEFAULT_TYPE, R.layout.layout_post_text_comment_item);
        addItemType(DataType.COMMENT_TEXT_TYPE, R.layout.layout_post_text_comment_item);
        addItemType(DataType.COMMENT_VOICE_TYPE, R.layout.layout_post_voice_comment_item);
    }

    @Override
    protected void convert(BaseViewHolder helper, CommentParent item) {
        handleCommentCommon(helper, item);
        switch (helper.getItemViewType()) {
            case DataType.COMMENT_DEFAULT_TYPE:
            case DataType.COMMENT_TEXT_TYPE:
                handleTextCommentContent(helper, item);
                break;
            case DataType.COMMENT_VOICE_TYPE:
                handleVoiceCommentContent(helper, item);
                break;
        }
    }

    /**
     * 绑定数据
     *
     * @param postId
     * @param postType
     */
    public void bindPostInfo(String postId, int postType) {
        mPostId = postId;
        mPostType = postType;
    }

    /**
     * 设置点赞控件内容
     *
     * @param isSelected
     * @param count
     */
    private void bindLikeCount(LikeButton likeButton, TextView tvPraise, boolean isSelected, int count) {
        likeButton.setLiked(isSelected);
        tvPraise.setSelected(isSelected);
        tvPraise.setText(CHelper.formatPageCount(count));
    }

    /**
     * 文本评论
     *
     * @param holder
     * @param commentParent
     */
    private void handleTextCommentContent(BaseViewHolder holder, CommentParent commentParent) {
        SimpleCommonUtils.spannableEmoticonFilter(holder
                .getView(R.id.tv_content), commentParent.getComment());
    }

    /**
     * 语音评论
     *
     * @param holder
     * @param commentParent
     */
    private void handleVoiceCommentContent(BaseViewHolder holder, CommentParent commentParent) {
        NVoiceBar nVoiceBar = holder.getView(R.id.nv_voice);
        nVoiceBar.setSmallStyle();
        nVoiceBar.setSexStyle(commentParent.getSex(), true);
        nVoiceBar.setVoiceLengthVisible(false);
        nVoiceBar.setData(commentParent.getComment(), 0f);
    }

    /**
     * 显示共用内容
     *
     * @param holder
     * @param commentParent
     */
    private void handleCommentCommon(final BaseViewHolder holder, final CommentParent commentParent) {
        ImageView avatarView = holder.getView(R.id.aiv_avatar);
        GlideUtils.loadSrcImage(ApUtils.getApp(), avatarView
                , CHelper.getOssThumbImageUrl(commentParent.getPhoto(), avatarSize));
        holder.setText(R.id.tv_floor, commentParent.getDate());
        TextView tvName = holder.getView(R.id.tv_name);
        tvName.setText(commentParent.getNickName());
        tvName.setCompoundDrawablesWithIntrinsicBounds(0, 0
                , RoleResLoader.getRoleDrawableResourceId(RoleResLoader.DynamicResName.COMMUNITY_SEX_ICON, commentParent.getSex()), 0);
        holder.setText(R.id.tv_time, commentParent.getDate());
        final LikeButton likeButton = holder.getView(R.id.lb_like);
        final TextView tvPraiseCount = holder.getView(R.id.tv_praise_count);
        bindLikeCount(likeButton, tvPraiseCount, commentParent.isBln(), commentParent.getLikeNum());
        CommentListView commentListView = holder.getView(R.id.clv_comment);
        commentListView.setDataList(commentParent);
        commentListView.setVisibility(commentParent.getCommentSub() == null
                || commentParent.getCommentSub().isEmpty() ? View.GONE : View.VISIBLE);
        likeButton.setOnLikeListener(new OnLikeListener() {
            @Override
            public void liked(LikeButton likeButton) {
                handleLikeStatus(true);
                CDataProcessor.doTopicPraise(commentParent
                        , commentParent.getCommentPkId(), CHelper.convertArticleType2PraiseType(mPostType, true));
            }

            @Override
            public void unLiked(LikeButton likeButton) {
                handleLikeStatus(false);
                CDataProcessor.doTopicPraise(commentParent
                        , commentParent.getCommentPkId(), CHelper.convertArticleType2PraiseType(mPostType, true));
            }

            private void handleLikeStatus(boolean add) {
                if (add) {
                    commentParent.addLikeNum();
                } else {
                    commentParent.reduceLikeNum();
                }
                tvPraiseCount.setText(CHelper.formatPageCount(commentParent.getLikeNum()));
                tvPraiseCount.setSelected(add);
            }

        });
        commentListView.setOnItemClickListener(new CommentListView.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                CommentSub commentSub = commentParent.getCommentSub().get(position);
                CommentSubEvent event = new CommentSubEvent();
                event.commentSubType = 2;
                event.autoInput = true;
                event.foreignPkId = mPostId;
                event.commentPkId = commentParent.getCommentPkId();
                event.fromUserId = commentSub.getFromeUserId();
                event.fromUserName = commentSub.getNickName();
                EventBus.getDefault().post(event);
            }
        });
        holder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommentSubEvent event = new CommentSubEvent();
                event.commentSubType = 1;
                event.autoInput = true;
                event.foreignPkId = mPostId;
                event.commentPkId = commentParent.getCommentPkId();
                event.fromUserId = commentParent.getUserId();
                event.fromUserName = commentParent.getNickName();
                EventBus.getDefault().post(event);
            }
        });
    }

}
